// pages/farm/unit/unit.js
var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    types: [],
    id:null,
    pdfUrl: api.PDFImageUrl,
    myPlatform:"ios",
    lef: null,
    rgt: null,
    level: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    let lv = parseInt(options.level);
    lv = lv + 1;//查下一级所以加一
    console.log(options);
    console.log(lv);
    that.setData({
      lef: options.lef,
      rgt: options.rgt,
      level: lv
    }) ,
      wx.getSystemInfo({
        success: function(res) {
          console.log("手机机型为："+res.platform);
          that.setData({
            
            myPlatform:res.platfor
          });
        },
        fail:function(){
          wx.showToast({
            title: '获取机型失败',
            icon: 'fail'
          })
        }
      })
 },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getQueryAll();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getQueryAll:function(){
      let that=this;
      console.log(that.data.id);
    util.request(api.FarmInfoChildNodes,{lef: that.data.lef, rgt: that.data.rgt, level: that.data.level}).then(function(res){
        console.log(res);
      if (res.errno === 0){
        that.setData({
          types: res.data
        });
      }
    });
  },
   /**
 * 下载pdf和查看pdf
 */
  downAndOpenPDF: function (event) {
    var filePath = event.currentTarget.dataset.s
    console.log("pdf接口链接" + filePath);
    wx.showLoading({
      title: '数据加载中',
      icon: 'fail'
    })
    //下载
    wx.downloadFile({
      url: filePath,
      success: function (res) {
        console.log("缓存链接"+res.tempFilePath);
        
          var pPath = res.tempFilePath;
          wx.openDocument({
            filePath: pPath,
          })
      },
      fail: function (t) {
        wx.showToast({
          title: '下载失败',
        })
      },
      complete: function (t) {
        wx.hideLoading()
      }
    })
  },
  /**
   * ios查看pdf
   */
  iosPDF:function(event){
    var filePath=event.currentTarget.dataset.s
    console.log("iosPdf链接：" + event.currentTarget.dataset.s);
    wx.navigateTo({
      url: "/pages/farm/iospdf/iospdf?filePath="+filePath
    });
  }
})